package com.banmao.greedysnake.handler;

import com.banmao.greedysnake.vo.Snake;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

// 方向键事件处理
@Slf4j
@Component
public class PositionKeyPressEventHandler implements EventHandler<KeyEvent> {

    private Snake snake;

    public void setSnake(Snake snake) {
        this.snake = snake;
    }

    @Override
    public void handle(KeyEvent event) {
        KeyCode code = event.getCode();
        log.info("点击了键盘 code:{}, name:{}", code.getCode(), code.getName());
        switch (code) {
            case UP -> {
                log.info("点击了UP");
                snake.setNextDirection(Snake.Direction.UP);
            }
            case DOWN -> {
                log.info("点击了DOWN");
                snake.setNextDirection(Snake.Direction.DOWN);
            }
            case LEFT -> {
                log.info("点击了LEFT");
                snake.setNextDirection(Snake.Direction.LEFT);
            }
            case RIGHT -> {
                log.info("点击了RIGHT");
                snake.setNextDirection(Snake.Direction.RIGHT);
            }
        }
    }
}